package com.ds.dsm.view.config.form;

import com.ds.common.JDSException;
import com.ds.config.ListResultModel;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.annotation.GridViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.bean.ApiClassConfig;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.buttonviews.annotation.ButtonViewsAnnotation;
import com.ds.esd.custom.form.CustomFormViewBean;
import com.ds.esd.custom.form.enums.CustomFormEvent;
import com.ds.esd.custom.form.enums.CustomFormMenu;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import com.ds.esd.tool.ui.component.tab.SideBarStatusType;
import com.ds.esd.tool.ui.enums.BarLocationType;
import com.ds.web.util.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RequestMapping("/dsm/view/config/form/")
@Controller
@ButtonViewsAnnotation(barLocation = BarLocationType.top, sideBarStatus = SideBarStatusType.expand, barSize = "3em")
@MethodChinaName(cname = "表单配置")
public class FormActionGroup {


    @CustomAnnotation(hidden = true, pid = true)
    String viewInstId;
    @CustomAnnotation(hidden = true, pid = true)
    String domainId;
    @CustomAnnotation(hidden = true, pid = true)
    String sourceClassName;
    @CustomAnnotation(hidden = true, uid = true)
    String methodName;


    public FormActionGroup() {

    }


    @RequestMapping(method = RequestMethod.POST, value = "TabCustomMenus")
    @GridViewAnnotation(
            addPath = "FormMenuTree")
    @ModuleAnnotation(caption = "工具栏",
            imageClass = "spafont spa-icon-c-menu")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<FormMenuView>> getTabCustomMenus(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ListResultModel<List<FormMenuView>> resultModel = new ListResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName,  viewInstId);
            MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            CustomFormViewBean formViewBean = (CustomFormViewBean) methodAPIBean.getView();
            Set<CustomFormMenu> customFormMenus = formViewBean.getCustomMenu();
            List<FormMenuView> views = new ArrayList<>();
            for (CustomFormMenu formMenu : customFormMenus) {
                views.add(new FormMenuView(formMenu, methodAPIBean));
            }

            resultModel = PageUtil.getDefaultPageList(views);
        } catch (JDSException e) {
            e.printStackTrace();
        }

        return resultModel;
    }

    @RequestMapping(method = RequestMethod.POST, value = "ToolBars")
    @GridViewAnnotation(
            addPath = "ToolBarsTree")
    @ModuleAnnotation(caption = "工具栏",
            imageClass = "spafont spa-icon-c-toolbar")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<FormMenuView>> getToolBars(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ListResultModel<List<FormMenuView>> resultModel = new ListResultModel<>();
        try {
            ViewEntityConfig classConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            ApiClassConfig customESDClassAPIBean = classConfig.getSourceConfig();
            MethodConfig methodAPIBean = customESDClassAPIBean.getMethodByName(methodName);
            CustomFormViewBean customFormViewBean = (CustomFormViewBean) methodAPIBean.getView();
            Set<CustomFormMenu> customMenu = customFormViewBean.getToolBarMenu();
            List<FormMenuView> views = new ArrayList<>();
            for (CustomFormMenu formMenu : customMenu) {
                views.add(new FormMenuView(formMenu, methodAPIBean));
            }
            resultModel = PageUtil.getDefaultPageList(views);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return resultModel;
    }


    @MethodChinaName(cname = "底部按钮组")
    @RequestMapping(method = RequestMethod.POST, value = "TabCustomBottombar")
    @GridViewAnnotation(addPath = "FormBottomTree")
    @ModuleAnnotation(caption = "底部按钮组", imageClass = "spafont spa-icon-c-statusbutton")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<FormMenuView>> getTabCustomBottombar(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ListResultModel<List<FormMenuView>> resultModel = new ListResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName,viewInstId);
            MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            CustomFormViewBean formViewBean = (CustomFormViewBean) methodAPIBean.getView();
            Set<CustomFormMenu> customFormMenus = formViewBean.getBottombarMenu();
            List<FormMenuView> views = new ArrayList<>();
            for (CustomFormMenu formMenu : customFormMenus) {
                views.add(new FormMenuView(formMenu, methodAPIBean));
            }
            resultModel = PageUtil.getDefaultPageList(views);
        } catch (JDSException e) {
            e.printStackTrace();
        }

        return resultModel;
    }

    @MethodChinaName(cname = "事件")
    @RequestMapping(method = RequestMethod.POST, value = "TabCustomFormEvents")
    @GridViewAnnotation()
    @ModuleAnnotation(caption = "事件", imageClass = "spafont spa-icon-event")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<FormEventView>> getTabCustomFormEvents(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ListResultModel<List<FormEventView>> resultModel = new ListResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            CustomFormViewBean formViewBean = (CustomFormViewBean) methodAPIBean.getView();
            Set<CustomFormEvent> events = formViewBean.getEvent();
            List<FormEventView> views = new ArrayList<>();
            for (CustomFormEvent formEvent : events) {
                views.add(new FormEventView(formEvent, methodAPIBean));
            }
            resultModel = PageUtil.getDefaultPageList(views);
        } catch (JDSException e) {
            e.printStackTrace();
        }


        return resultModel;
    }

    public String getDomainId() {
        return domainId;
    }

    public void setDomainId(String domainId) {
        this.domainId = domainId;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getViewInstId() {
        return viewInstId;
    }

    public void setViewInstId(String viewInstId) {
        this.viewInstId = viewInstId;
    }

    public String getSourceClassName() {
        return sourceClassName;
    }

    public void setSourceClassName(String sourceClassName) {
        this.sourceClassName = sourceClassName;
    }
}
